package cn.com.css.taxInspection.gt3.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import cn.com.css.taxInspection.attention.entity.AjcxRecord;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.gt3.entity.ZxyhjEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.css.taxInspection.gt3.entity.DmJcAjztEntity;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.entity.JcLsfxMxEntity;
import cn.com.css.taxInspection.gt3.mapper.JcAjxxMapper;
import cn.com.css.taxInspection.gt3.service.DmJcAjztService;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.gt3.service.JcLsfxMxService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.entity.WorkflowInspectionHearEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowNotificationEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowInspectionHearService;
import cn.com.css.taxInspection.workflow.service.WorkflowNotificationService;

@Service
public class JcAjxxServiceImpl extends ServiceImpl<JcAjxxMapper, JcAjxxEntity> implements JcAjxxService {
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private DmGySwryService dmGySwryService;
    @Resource
    private WorkflowInspectionHearService workflowInspectionHearService;
    @Resource
    private JcLsfxMxService jcLsfxMxService;
    @Resource
    private WorkflowNotificationService workflowNotificationService;
    @Resource
    private DmJcAjztService dmJcAjztService;
    @Resource
    private JcAjxxMapper jcAjxxMapper;

    @Override
    public List<JcAjxxEntity> pageInfo11(WorkflowInspectionHearEntity workflowInspectionHear) {
        // TODO 检查中
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        List<String> listState = new ArrayList<>();
        listState.add(WorkflowConstants.INSPECT_121);
        listState.add(WorkflowConstants.INSPECT_131);
        listState.add(WorkflowConstants.INSPECT_11);
        listState.add(WorkflowConstants.INSPECT_14);
        listState.add(WorkflowConstants.HEAR_211);
        List<List<WorkflowInspectionHearEntity>> list = workflowInspectionHearService
                .getByStateCodeAndInspectionMan11(listState, currentUserId, currentDeptId);
        List<WorkflowInspectionHearEntity> _list = list.get(0);
        List<WorkflowInspectionHearEntity> _listAll = list.get(1);
        List<String> relevanceList = new ArrayList<>();
        List<JcAjxxEntity> jcAjxxEntityList = new ArrayList<>();
        for (WorkflowInspectionHearEntity inspectionHear : _list) {
            String relevance = inspectionHear.getRelevance();
            JcAjxxEntity jcAjxxEntity = this.getById(relevance);
            jcAjxxEntity.setStateCode(inspectionHear.getStateCode());
            jcAjxxEntity.setStateName(inspectionHear.getStateName());
            jcAjxxEntityList.add(jcAjxxEntity);
        }
        for (WorkflowInspectionHearEntity inspectionHear : _listAll) {
            String relevance = inspectionHear.getRelevance();
            relevanceList.add(relevance);
        }
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(JcAjxxEntity::getJcbmDm, currentDeptId);
        queryWrapper.likeRight(JcAjxxEntity::getAjjcztDm, "2");
        queryWrapper.eq(JcAjxxEntity::getZfbz_1, "N");
        if (!relevanceList.isEmpty()) {
            queryWrapper.notIn(JcAjxxEntity::getJcajxxuuid, relevanceList);
        }
        queryWrapper.like(JcAjxxEntity::getJcry, currentUserId);
        queryWrapper.orderByDesc(JcAjxxEntity::getLrrq);
        List<JcAjxxEntity> list2 = this.list(queryWrapper);
        for (JcAjxxEntity jcAjxxEntity : list2) {
            jcAjxxEntity.setStateCode(WorkflowConstants.INSPECT_99);
            jcAjxxEntity.setStateName(WorkflowConstants.INSPECT_99_NAME);
            jcAjxxEntityList.add(jcAjxxEntity);
        }
        return jcAjxxEntityList;
    }

    @Override
    public List<JcAjxxEntity> pageInfo21(WorkflowInspectionHearEntity workflowInspectionHear) {
        // TODO 检查中
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        List<String> listStateCode = new ArrayList<>();
        listStateCode.add(WorkflowConstants.HEAR_221);
        listStateCode.add(WorkflowConstants.HEAR_231);
        listStateCode.add(WorkflowConstants.HEAR_241);
        listStateCode.add(WorkflowConstants.HEAR_21);
        listStateCode.add(WorkflowConstants.HEAR_25);
        listStateCode.add(WorkflowConstants.HEAR_20);
        listStateCode.add(WorkflowConstants.HEAR_2105);
        listStateCode.add(WorkflowConstants.HEAR_2106);
        listStateCode.add(WorkflowConstants.HEAR_2107);
        listStateCode.add(WorkflowConstants.HEAR_2108);
        List<List<WorkflowInspectionHearEntity>> list = workflowInspectionHearService.getByStateCodeAndInspectionMan21(listStateCode, currentUserId);
        List<WorkflowInspectionHearEntity> _list = list.get(0);
        List<WorkflowInspectionHearEntity> _listAll = list.get(1);
        List<String> relevanceList = new ArrayList<>();
        List<JcAjxxEntity> jcAjxxEntityList = new ArrayList<>();
        if (TsBqUtils.isNotNull(_list)) {
            for (WorkflowInspectionHearEntity inspectionHear : _list) {
                String relevance = inspectionHear.getRelevance();
                JcAjxxEntity jcAjxxEntity = this.getById(relevance);
                jcAjxxEntity.setStateCode(inspectionHear.getStateCode());
                jcAjxxEntity.setStateName(inspectionHear.getStateName());
                jcAjxxEntityList.add(jcAjxxEntity);
            }
        }
        if (TsBqUtils.isNotNull(_listAll)) {
            for (WorkflowInspectionHearEntity inspectionHear : _listAll) {
                String relevance = inspectionHear.getRelevance();
                relevanceList.add(relevance);
            }
        }
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(JcAjxxEntity::getSlbmDm, currentDeptId);
        queryWrapper.likeRight(JcAjxxEntity::getAjjcztDm, "3");
        queryWrapper.like(JcAjxxEntity::getSlry, currentUserId);
        if (TsBqUtils.isNotNull(relevanceList)) {
            queryWrapper.notIn(JcAjxxEntity::getJcajxxuuid, relevanceList);
        }
        queryWrapper.orderByDesc(JcAjxxEntity::getLrrq);
        List<JcAjxxEntity> list2 = this.list(queryWrapper);
        for (JcAjxxEntity jcAjxxEntity : list2) {
            jcAjxxEntity.setStateCode(WorkflowConstants.HEAR_99);
            jcAjxxEntity.setStateName(WorkflowConstants.HEAR_99_NAME);
            jcAjxxEntityList.add(jcAjxxEntity);
        }
        return jcAjxxEntityList;
    }

    @Override
    public JcAjxxEntity getByJcajxxuuid(String jcajxxuuid) {
        return this.getById(jcajxxuuid);
    }

    @Override
    public List<JcAjxxEntity> getByRisk() {
        List<JcAjxxEntity> jcAjxxEntityList = new ArrayList<>();
        //  TODO
        // workflowThresholdDetailService.getById(id)
        String currentUserId = httpSessionService.getCurrentUserId();
        LambdaQueryWrapper<JcLsfxMxEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(JcLsfxMxEntity::getYxbz, "Y");
        List<JcLsfxMxEntity> list = jcLsfxMxService.list(queryWrapper);
        if (TsBqUtils.isNotNull(list)) {
            for (JcLsfxMxEntity jcLsfxMxEntity : list) {
                String relevance = jcLsfxMxEntity.getRelevance();
                String riskName = jcLsfxMxEntity.getRiskName();
                JcAjxxEntity jcAjxxEntity = this.getById(relevance);
                jcAjxxEntity.setRiskName(riskName);
                String jcry = jcAjxxEntity.getJcry();
                if (TsBqUtils.isNotNull(jcry)) {
                    if (jcry.contains(currentUserId)) {

                        jcAjxxEntityList.add(jcAjxxEntity);

                    }
                }
            }
        }
        return jcAjxxEntityList;
    }

    @Override
    public JcAjxxEntity getByJcajbh(String jcajbh) {
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(JcAjxxEntity::getJcajbh, jcajbh);
        queryWrapper.orderByAsc(JcAjxxEntity::getLrrq);
        List<JcAjxxEntity> list = this.list(queryWrapper);
        if (TsBqUtils.isNotNull(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<JcAjxxEntity> pageInfoByNotification0(WorkflowNotificationEntity workflowNotification) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentUserId = httpSessionService.getCurrentUserId();
        List<WorkflowNotificationEntity> workflowNotificationEntityList = workflowNotificationService.getByCreateId(currentUserId);
        List<String> jcuuidList = new ArrayList<>();
        if (TsBqUtils.isNotNull(workflowNotificationEntityList)) {
            for (WorkflowNotificationEntity workflowNotificationEntity : workflowNotificationEntityList) {
                jcuuidList.add(workflowNotificationEntity.getRelevance());
            }
        }
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.like(JcAjxxEntity::getJcry, currentUserId);
        if (!StringUtils.isEmpty(workflowNotification.getStartTime())) {
            queryWrapper.gt(JcAjxxEntity::getLarq, sdf.parse(workflowNotification.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowNotification.getEndTime())) {
            queryWrapper.lt(JcAjxxEntity::getLarq, sdf.parse(workflowNotification.getEndTime()));
        }
        if (!StringUtils.isEmpty(workflowNotification.getAjbh())) {
            queryWrapper.eq(JcAjxxEntity::getJcajbh, workflowNotification.getAjbh());
        }
        if (TsBqUtils.isNotNull(jcuuidList)) {
            queryWrapper.notIn(JcAjxxEntity::getJcajxxuuid, jcuuidList);
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<JcAjxxEntity> getByJcAjxx(JcAjxxEntity jcAjxx) {
        String nsrmc = jcAjxx.getNsrmc();
        String nsrsbh = jcAjxx.getNsrsbh();
        String jcajbh = jcAjxx.getJcajbh();
        String jcry = jcAjxx.getJcry();
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(nsrmc)) {
            queryWrapper.eq(JcAjxxEntity::getNsrmc, nsrmc);
        }
        if (TsBqUtils.isNotNull(nsrsbh)) {
            queryWrapper.eq(JcAjxxEntity::getNsrsbh, nsrsbh);
        }
        if (TsBqUtils.isNotNull(jcajbh)) {
            queryWrapper.eq(JcAjxxEntity::getJcajbh, jcajbh);
        }
        if (TsBqUtils.isNotNull(jcry)) {
            queryWrapper.like(JcAjxxEntity::getJcry, jcry);
        }
        return this.list(queryWrapper);
    }

    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    public IPage<JcAjxxEntity> pageInfoByJcrAndSlrAndZxr(
            WorkflowInspectionHearEntity workflowInspectionHear, String currentUserId) {
        String currentRolesId = httpSessionService.getCurrentRolesId();
        String nsrmc = workflowInspectionHear.getNsrmc();
        String nsrsbh = workflowInspectionHear.getNsrsbh();
        String jcajbh = workflowInspectionHear.getJcajbh();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        if (TsBqUtils.isNotNull(nsrmc)) {
            queryWrapper.like(JcAjxxEntity::getNsrmc, nsrmc);
        }
        if (TsBqUtils.isNotNull(nsrsbh)) {
            queryWrapper.like(JcAjxxEntity::getNsrsbh, nsrsbh);
        }
        if (TsBqUtils.isNotNull(jcajbh)) {
            queryWrapper.like(JcAjxxEntity::getJcajbh, jcajbh);
        }
        boolean containsJcry = currentRolesId.contains(WorkflowConstants.ROLE_1708113478976823298); // 检查-检查人岗位
        boolean containsSlry = currentRolesId.contains(WorkflowConstants.ROLE_1708113854186676225); // 审理-审理员岗位
        boolean containsZxry = currentRolesId.contains(WorkflowConstants.ROLE_1750438346513281026); // 执行-执行员岗位
        if (containsJcry) {
            queryWrapper.like(JcAjxxEntity::getJcry, currentUserId);
        }
        if (containsSlry) {
            queryWrapper.like(JcAjxxEntity::getSlry, currentUserId);
        }
        if (containsZxry) {
            queryWrapper.like(JcAjxxEntity::getZxry, currentUserId);
        }
        if (!TsBqUtils.isNull(workflowInspectionHear.getAjztDms())) {
            List<String> asList = Collections.singletonList(workflowInspectionHear.getAjztDms());
            for (int i = 0; i < asList.size(); i++) {
                String tmpajztdm = asList.get(i);
                if (tmpajztdm.indexOf("901") != -1) {
                    if ("901".equals(tmpajztdm)) {
                        continue;
                    }
                    queryWrapper.or().likeRight(JcAjxxEntity::getAjjcztDm, tmpajztdm);
                } else {
                    if (i == 0) {
                        queryWrapper.likeRight(JcAjxxEntity::getAjjcztDm, tmpajztdm);
                    } else {
                        queryWrapper.or().likeRight(JcAjxxEntity::getAjjcztDm, tmpajztdm);
                    }
                }
            }
        }
        IPage<JcAjxxEntity> iPage = this.page(page, queryWrapper);
        List<JcAjxxEntity> records = iPage.getRecords();
        for (JcAjxxEntity jcAjxxEntity : records) {
            String ajjczt = jcAjxxEntity.getAjjcztDm();
            DmJcAjztEntity dmJcAjzt = dmJcAjztService.getByAjztDm(ajjczt);
            if (TsBqUtils.isNotNull(dmJcAjzt)) {
                ajjczt = dmJcAjzt.getAjztmc();
            }
            jcAjxxEntity.setAjjcztDm(ajjczt);
        }
        return iPage;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public IPage<JcAjxxEntity> pageInfo(JcAjxxEntity jcAjxxEntity) {
        Page page = new Page(jcAjxxEntity.getPage(), jcAjxxEntity.getLimit());
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        String jcajbh = jcAjxxEntity.getJcajbh();
        // 查询条件示例
        if (TsBqUtils.isNotNull(jcajbh)) {
            queryWrapper.eq(JcAjxxEntity::getJcajbh, jcajbh);
        }
        return this.page(page, queryWrapper);
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public IPage<JcAjxxEntity> queryAjxxList(JcAjxxEntity jcAjxxEntity) {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(jcAjxxEntity.getPage(), jcAjxxEntity.getLimit());
        LambdaQueryWrapper<JcAjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        // 案件编号
        if (TsBqUtils.isNotNull(jcAjxxEntity.getJcajbh())) {
            queryWrapper.like(JcAjxxEntity::getJcajbh, jcAjxxEntity.getJcajbh());
        }
        // 纳税人名称
        if (!StringUtils.isEmpty(jcAjxxEntity.getNsrmc())) {
            queryWrapper.like(JcAjxxEntity::getNsrmc, jcAjxxEntity.getNsrmc());
        }
        // 纳税人识别号
        if (TsBqUtils.isNotNull(jcAjxxEntity.getNsrsbh())) {
            queryWrapper.like(JcAjxxEntity::getNsrsbh, jcAjxxEntity.getNsrsbh());
        }
        // 案件状态
        if (TsBqUtils.isNotNull(jcAjxxEntity.getAjjcztDm())) {
            String[] list = jcAjxxEntity.getAjjcztDm().split(","); // 按逗号分割并转换成 List
            for (String str : list) {
                queryWrapper.or().likeRight(JcAjxxEntity::getAjjcztDm, str);
            }
        }
        // 全市和全局
        if (jcAjxxEntity.getBz().equals(Constants.LEVEL_SHENGJU)) {
            queryWrapper.like(JcAjxxEntity::getAjszjgDm, currentDeptId.substring(0, 8));
        } else {
            queryWrapper.like(JcAjxxEntity::getAjszjgDm, currentDeptId.substring(0, 9));
        }
        queryWrapper.orderByDesc(JcAjxxEntity::getLarq);
        IPage<JcAjxxEntity> iPage = this.page(page, queryWrapper);
        List<JcAjxxEntity> jcAjxxEntityList = iPage.getRecords();
        for (JcAjxxEntity ajxxEntity : jcAjxxEntityList) {
            String ajjczt = ajxxEntity.getAjjcztDm();
            DmJcAjztEntity dmJcAjzt = dmJcAjztService.getByAjztDm(ajjczt);
            if (TsBqUtils.isNotNull(dmJcAjzt)) {
                ajjczt = dmJcAjzt.getAjztmc();
            }
            ajxxEntity.setAjjcztDm(ajjczt);
        }
        return iPage;
    }

    @Override
    public List<ZxyhjEntity> zxyzxhjgrdb(WorkflowInspectionHearEntity workflowInspectionHear) {
        String swryxm = dmGySwryService.getSwry(httpSessionService.getCurrentUserId()).getSwryxm();
        return jcAjxxMapper.queryAjxxByAjztDmAndSwryXm("402", swryxm);
    }
}
